/** @description 数据缓存存储 **/
/** @author  wutao **/
/** @date 2020-04-20 **/
let storage = {};

storage.storageApi = {
	badgeNum: 'badge_num', //应用角标数
	token: 'storage_nsy_token', //token
	searchHis: 'search_his',
	userInfo: 'storage_nsy_userInfo',
	accountsInfo: 'storage_accountsInfo', //登录账号密码
}
//数据存储在本地缓存中指定的 key 中 异步
storage.setStorage = (key, data) => {
	return uni.setStorageSync(key, data);
	// return new Promise((resolve, reject) => {
	// 	uni.setStorage({
	// 		key: key,
	// 		data: data,
	// 		success: function() {
	// 			resolve();
	// 			// console.log('success');
	// 		},
	// 		fail: (res) => {
	// 			reject();
	// 			console.log('fail====>');
	// 		}
	// 	});
	// });

}

/**  从本地缓存中异步获取指定 key 对应的内容 同步。**/
storage.getStorageSync = (key) => {
	try {
		let value = uni.getStorageSync(key);
		if (value) {
			// console.log('storage:' + key + '=====>' + value);
			return value;
		} else {
			return null;
		}
	} catch (e) {
		// error
		return null;
	}
}
//删除指定key
storage.removeStorage = (key) => {
	uni.removeStorage({
		key: key,
		success: function(res) {
			console.log('remove storage ' + key + 'success');
		}
	});
}
//删除多个key
storage.removeStorages = (keys) => {
	if (keys instanceof Array) {
		keys.forEach(key => {
			storage.removeStorage(key);
		})
	}
}

//token
storage.setToken = (token) => {
	return storage.setStorage(storage.storageApi.token, token);
}
storage.getToken = () => {
	return storage.getStorageSync(storage.storageApi.token);
}
storage.removeToken = () => {
	storage.removeStorage(storage.storageApi.token);
}
// 用户个人信息
storage.setUserInfo = (userInfo) => {
	return storage.setStorage(storage.storageApi.userInfo, userInfo);
}
storage.getUserInfo = () => {
	return storage.getStorageSync(storage.storageApi.userInfo);
}
storage.removeUserInfo = () => {
	storage.removeStorage(storage.storageApi.userInfo);
}

// 用户账号密码信息
storage.setAccountsInfo = (accountsInfo) => {
	return storage.setStorage(storage.storageApi.accountsInfo, accountsInfo);
}
storage.getAccountsInfo = () => {
	return storage.getStorageSync(storage.storageApi.accountsInfo);
}
storage.removeAccountsInfo = () => {
	storage.removeStorage(storage.storageApi.accountsInfo);
}


//searchHis
storage.setSearchHis = (searchHis) => {
	storage.setStorage(storage.storageApi.searchHis, searchHis);
}
storage.getSearchHis = () => {
	return storage.getStorageSync(storage.storageApi.searchHis);
}
storage.removeSearchHis = () => {
	storage.removeStorage(storage.storageApi.searchHis);
}

//addressList
storage.setAddressList = (addressList) => {
	return storage.setStorage(storage.storageApi.addressList, addressList);
}
storage.getAddressList = () => {
	return storage.getStorageSync(storage.storageApi.addressList);
}
storage.removeAddressList = () => {
	storage.removeStorage(storage.storageApi.addressList);
}

//应用角标
storage.getBadgeNumStorage = () => {
	return (storage.getStorageSync(storage.storageApi.badgeNum) || 0);
}
storage.setBadgeNumStorage = (num) => {
	storage.setStorage(storage.storageApi.badgeNum, num);
}
//启动广告
storage.setOpenGif = (openGif) => {
	return storage.setStorage(storage.storageApi.openGif, openGif);
}
storage.getOpenGif = () => {
	return storage.getStorageSync(storage.storageApi.openGif);
}
storage.removeOpenGif = () => {
	storage.removeStorage(storage.storageApi.openGif);
}
//清理所有
storage.clearAllStorage = () => {
	uni.clearStorage();
}


export default storage
